/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.awt.Component;
import java.awt.Container;
import java.util.Date;
import javax.swing.*;

/**
 *
 * @author menderleit
 */
public abstract class GUIUtils {
    
    public static void clearFields(Container parent) {
        parent.validate();
        Component[] c = parent.getComponents();
        for (int i = 0; i < c.length; i++) {
            Component comp = c[i];
            if (comp != null) {
                if (comp instanceof JPanel) {
                    clearFields((JPanel) comp);
                } else if (comp instanceof JScrollPane) {
                    clearFields((JScrollPane) comp);
                } else if (comp instanceof JViewport) {
                    clearFields((JViewport) comp);
                }
                if (comp instanceof JTextField) {
                    JTextField txtf = (JTextField) comp;
                    txtf.setText("");
                }
                if (comp instanceof JTextArea) {
                    JTextArea txtf = (JTextArea) comp;
                    txtf.setText("");
                }
                if (comp instanceof JCheckBox) {
                    JCheckBox sp = (JCheckBox) comp;
                    sp.setSelected(false);
                }
                if (comp instanceof JList) {
                    JList sl = (JList) comp;
                    Object[] emptyArray = new Object[0];
                    sl.setListData(emptyArray);
                }
                if (comp instanceof JComboBox) {
                    JComboBox sl = (JComboBox) comp;
                    sl.setSelectedIndex(0);
                }
                if (comp instanceof JSpinner) {
                    SpinnerModel model = ((JSpinner) comp).getModel();
                    if (model instanceof SpinnerDateModel) {
                        SpinnerDateModel sdm = (SpinnerDateModel) model;
                        sdm.setValue(new Date());
                    } else if (model instanceof SpinnerNumberModel) {
                        SpinnerNumberModel snm = (SpinnerNumberModel) model;
                        snm.setValue(new Integer(0));
                    }
                }
            } else {
                System.out.println("null");
            }
        }
    }
}
